#include "../../include/offlinedict/CppJieba.h"
#include "../../include/offlinedict/Configuration.h"

CppJieba *CppJieba::_pInstance = nullptr;
pthread_once_t CppJieba::_once = PTHREAD_ONCE_INIT;

CppJieba *CppJieba::getInstance()
{
    pthread_once(&_once,init_r);
    return _pInstance;
}

void CppJieba::init_r()
{
    if(_pInstance == nullptr)
    {
        _pInstance = new CppJieba();
        atexit(destory);
    }
}

vector<string> CppJieba::cut(const string &word) 
{
    vector<string> result;
    _jieba.Cut(word,result);
    return result;
}

CppJieba::CppJieba()
:_jieba(Configuration::getInstance()->getMapValue("DICT_PATH"),
        Configuration::getInstance()->getMapValue("HMM_PATH"),
        Configuration::getInstance()->getMapValue("USER_DICT_PATH"),
        Configuration::getInstance()->getMapValue("IDF_PATH"),
        Configuration::getInstance()->getMapValue("STOP_WORD_PATH"))
{

}

CppJieba::~CppJieba()
{

}

void CppJieba::destory()
{
    if(_pInstance)
    {
        delete _pInstance;
        _pInstance = nullptr;
    }
}